package com.google.project.server;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.google.project.client.Service.TaskRequireService;
import com.google.project.server.entity.Project;
import com.google.project.server.entity.ProjectTask;
import com.google.project.server.entity.TaskRequire;
import com.google.project.shared.ProjectDTO;
import com.google.project.shared.ProjectTaskDTO;
import com.google.project.shared.TaskRequireDTO;

@SuppressWarnings("serial")
public class TaskRequireServiceImpl extends RemoteServiceServlet implements
		TaskRequireService {

	@SuppressWarnings("unchecked")
	@Override
	public List<TaskRequireDTO> getListTaskReq() {
		List<TaskRequireDTO> taskList = new ArrayList<TaskRequireDTO>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(TaskRequire.class);
		try {
			List<TaskRequire> list = (List<TaskRequire>) q.execute();
			for (TaskRequire taskReq : list) {
				taskList.add(taskReq.toDTO());
			}
		} catch (Exception e) {
			e.getMessage();
		} finally {
			pm.close();
		}
		return taskList;

	}
	
	
	@Override
	public int getNumEmpOfProject(String projectId) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Project projectInfo;
		int numEmp = 0;
		try {
			projectInfo = pm.getObjectById(Project.class, projectId);
			if (projectInfo != null && projectInfo.getListOfTask().size() > 0) {
				for (ProjectTask task : projectInfo.getListOfTask()) {
					numEmp += getNumEmpOfTask(task.getId());
				}
			}
		} catch (Exception e) {
			e.getMessage();
		} finally {
			pm.close();
		}
		return numEmp;
	}
	public int getNumEmpOfTask(String taskId) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		int numEmp = 0;
		try {
			ProjectTask task=pm.getObjectById(ProjectTask.class,taskId);
			for (TaskRequire taskReq : task.getListOfTaskRequire()) {
				if(taskReq.getNumOfEmp()>0){
				  numEmp += taskReq.getNumOfEmp();
				}
			}
		} catch (Exception e) {
			e.getMessage();
		}
		return numEmp;
	}
	// add TaskReq
	@Override
	public void addTaskReq(ProjectTaskDTO task, TaskRequireDTO taskReqDTO) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		TaskRequire taskReq = null;
		boolean flag=true;
		try {
			    ProjectTask ProTask = pm.getObjectById(ProjectTask.class, task.getId());
				 Project proj = pm.getObjectById(Project.class, task.getProjectId());
			    
			  //update số lượng nhân sự yêu cầu
				int numOfReq=taskReqDTO.getNumOfEmp()+proj.getNumOfEmpReq();
				ProjectDTO proDTO=proj.toDTO();
				proDTO.setNumOfEmpReq(numOfReq);
				 proj.updateFromDTO(proDTO);
				 //update taskReq
				 for (TaskRequire taskR : ProTask.getListOfTaskRequire()) {
						if(taskReqDTO.getSpecializeId().equals(taskR.getSpecializeId()) && taskReqDTO.getExperience()==taskR.getExperience() ){
							int numOfEmp=taskR.getNumOfEmp()+taskReqDTO.getNumOfEmp();
							taskReqDTO.setNumOfEmp(numOfEmp);
							taskR.updateFromDTO(taskReqDTO);//update yêu cầu nhân sự
							 flag=false;
							 break;
						}
					}
				    if(flag==true){
				    	//Tạo mới yêu cầu nhân sự
				    	taskReq = new TaskRequire(taskReqDTO);
						ProTask.getListOfTaskRequire().add(taskReq);
						pm.makePersistent(ProTask);
						
				    }
				
		} finally {
			pm.close();
		}

	}

	@Override
	public List<TaskRequireDTO> getTaskReqByTaskId(String taskId) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		ProjectTask task = null;
		List<TaskRequireDTO> listTaskReq = new ArrayList<TaskRequireDTO>();
		try {
			task = pm.getObjectById(ProjectTask.class, taskId);
			if (task.getListOfTaskRequire() != null
					&& task.getListOfTaskRequire().size() > 0) {
				for (TaskRequire taskRequire : task.getListOfTaskRequire()) {
					listTaskReq.add(taskRequire.toDTO());
				}
			}
		} catch (Exception ex) {
			ex.getMessage();
		} finally {
			pm.close();
		}
		return listTaskReq;
	}
	@Override
	public void deleteTaskReqList(List<TaskRequireDTO> list) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			for (TaskRequireDTO taskReqDTO : list) {
			
				TaskRequire taskReq = pm.getObjectById(TaskRequire.class, taskReqDTO.getId());
				//update lại số lượng nhân viên yêu cầu trong bảng project
				 Project proj = pm.getObjectById(Project.class, taskReqDTO.getProjectId());
				int numOfReq=proj.getNumOfEmpReq()-taskReq.getNumOfEmp();
				ProjectDTO projectDTO=proj.toDTO();
				projectDTO.setNumOfEmpReq(numOfReq);
				proj.updateFromDTO(projectDTO);
				//xóa trong bảng TaskReq
				if (taskReq != null) {
					pm.deletePersistent(taskReq);
				}
			}
		
		} finally {
			pm.close();
		}
	}
	//lấy về tất cả task ko có cha kèm theo số lượng nhân viên yêu cầu theo projectId
	
	@Override
	public List<ProjectTaskDTO> getListTask(String projectId) {
		List<ProjectTaskDTO> listTaskDTO=new ArrayList<ProjectTaskDTO>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			
			Project currProject=pm.getObjectById(Project.class,projectId);
			for(ProjectTask listTask : currProject.getListOfTask()){
				if(listTask.getSubTasks().equalsIgnoreCase("") || listTask.getSubTasks()==null){//lấy về tất cả task ko có cha 
					ProjectTaskDTO taskDTO=listTask.toDTO();
					int num=getNumEmpOfTask(taskDTO.getId());
					if(num>0){
						taskDTO.setNumOfEmp(num); //số lượng nhân viên yêu cầu theo taskId
						listTaskDTO.add(taskDTO);
					}
					
				}
			}
		} catch (Exception e) {
			e.getMessage();
		} finally {
			pm.close();
		}
		return listTaskDTO;
	}
}
